package com.hdu.xxlJobLocalCacheFix.req;

import com.hdu.xxlJobLocalCacheFix.enums.CacheType;
import com.hdu.xxlJobLocalCacheFix.enums.OpType;
import lombok.Data;

import static com.alibaba.fastjson2.JSON.toJSONString;


@Data
public class LocalCacheFixReq {
    private CacheType cacheType;
    private String cacheName;
    private String cacheKey;
    private String cacheKeyType;
    private String cacheValue;
    private String cacheValueType;
    private OpType opType;

    public static void main(String[] args) {
        //{"cacheKey":"\"testKey\"","cacheKeyType":"java.lang.String","cacheName":"id2nameCache","cacheValue":"\"testValue\"","cacheValueType":"java.lang.String","opType":2}
        //{"cacheName":"id2nameCache","opType":0}
        //{"cacheKey":"\"testKey\"","cacheKeyType":"java.lang.String","cacheName":"id2nameCache","opType":1}
        //{"cacheKey":"\"testKey1\"","cacheKeyType":"java.lang.String","cacheName":"id2nameCache","cacheValue":"\"newTestValue\"","cacheValueType":"java.lang.String","opType":5}
        //{"cacheKey":"\"testKey\"","cacheKeyType":"java.lang.String","cacheName":"id2nameCache","cacheValue":"\"newTestValue\"","cacheValueType":"java.lang.String","opType":5}
        //{"cacheName":"id2nameCache","opType":0}
        //{"cacheKey":"\"testKey\"","cacheKeyType":"java.lang.String","cacheName":"id2nameCache","opType":1}
        //{"cacheKey":"\"testKey\"","cacheKeyType":"java.lang.String","cacheName":"id2nameCache","opType":3}
        //{"cacheName":"id2nameCache","opType":0}
        System.out.println(generatePutReq("id2nameCache", "testKey", "testValue"));
        System.out.println(generateListAllReq("id2nameCache"));
        System.out.println(generateGetReq("id2nameCache", "testKey"));
        System.out.println(generateUpdateReq("id2nameCache", "testKey1", "newTestValue"));
        System.out.println(generateUpdateReq("id2nameCache", "testKey", "newTestValue"));
        System.out.println(generateListAllReq("id2nameCache"));
        System.out.println(generateGetReq("id2nameCache", "testKey"));
        System.out.println(generateDeleteReq("id2nameCache", "testKey"));
        System.out.println(generateListAllReq("id2nameCache"));
    }

    private static String generatePutReq(String cacheName, Object cacheKey, Object cacheValue) {
        LocalCacheFixReq localCacheFixReq = new LocalCacheFixReq();
        localCacheFixReq.setCacheName(cacheName);
        localCacheFixReq.setOpType(OpType.PUT);
        localCacheFixReq.setCacheKey(toJSONString(cacheKey));
        localCacheFixReq.setCacheKeyType(cacheKey.getClass().getName());
        localCacheFixReq.setCacheValue(toJSONString(cacheValue));
        localCacheFixReq.setCacheValueType(cacheValue.getClass().getName());
        return toJSONString(localCacheFixReq);
    }

    private static String generateDeleteReq(String cacheName, Object cacheKey) {
        LocalCacheFixReq localCacheFixReq = new LocalCacheFixReq();
        localCacheFixReq.setCacheName(cacheName);
        localCacheFixReq.setOpType(OpType.DELETE);
        localCacheFixReq.setCacheKey(toJSONString(cacheKey));
        localCacheFixReq.setCacheKeyType(cacheKey.getClass().getName());
        return toJSONString(localCacheFixReq);
    }

    private static String generateClearReq(String cacheName) {
        LocalCacheFixReq localCacheFixReq = new LocalCacheFixReq();
        localCacheFixReq.setCacheName(cacheName);
        localCacheFixReq.setOpType(OpType.CLEAR);
        return toJSONString(localCacheFixReq);
    }

    private static String generateGetReq(String cacheName, Object cacheKey) {
        LocalCacheFixReq localCacheFixReq = new LocalCacheFixReq();
        localCacheFixReq.setCacheName(cacheName);
        localCacheFixReq.setOpType(OpType.GET);
        localCacheFixReq.setCacheKey(toJSONString(cacheKey));
        localCacheFixReq.setCacheKeyType(cacheKey.getClass().getName());
        return toJSONString(localCacheFixReq);
    }

    private static String generateListAllReq(String cacheName) {
        LocalCacheFixReq localCacheFixReq = new LocalCacheFixReq();
        localCacheFixReq.setCacheName(cacheName);
        localCacheFixReq.setOpType(OpType.LIST_ALL);
        return toJSONString(localCacheFixReq);
    }

    private static String generateUpdateReq(String cacheName, Object cacheKey, Object cacheValue) {
        LocalCacheFixReq localCacheFixReq = new LocalCacheFixReq();
        localCacheFixReq.setCacheName(cacheName);
        localCacheFixReq.setOpType(OpType.UPDATE);
        localCacheFixReq.setCacheKey(toJSONString(cacheKey));
        localCacheFixReq.setCacheKeyType(cacheKey.getClass().getName());
        localCacheFixReq.setCacheValue(toJSONString(cacheValue));
        localCacheFixReq.setCacheValueType(cacheValue.getClass().getName());
        return toJSONString(localCacheFixReq);
    }
}
